package com.zx._07_数据库.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class 创建表 {

    public static void main(String[] args) throws Exception {
        // 创建表
        //createTable();

        // 删除表
        //dropTable();
    }

/*
	CREATE TABLE `t_member` (
	  `id` bigint(20) unsigned NOT NULL,
	  `real_name` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '真实姓名',
	  `cell_phone` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '手机',
	  `university_name` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '大学',
	  `city` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '城市',
	  `street` varchar(128) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '地址',
	  PRIMARY KEY (`id`)
	) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin COMMENT='会员-分表';


		TRUNCATE TABLE t_member_0;
		TRUNCATE TABLE t_member_1;
		TRUNCATE TABLE t_member_2;
		TRUNCATE TABLE t_member_3;
		TRUNCATE TABLE t_member_4;
		TRUNCATE TABLE t_member_5;
		TRUNCATE TABLE t_member_6;
		TRUNCATE TABLE t_member_7;
		TRUNCATE TABLE t_member_8;
		TRUNCATE TABLE t_member_9;
*/

    public static void createTable() {
        // 创建表
        try (Connection con = JDBC.getConnection()) {
            for (int i = 0; i < Mate.TABLE_NUM; i++) {
                String sql = "create table t_member_" + i + " like t_member;";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("table create sql=" + sql);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void dropTable() {
        // 创建表
        try (Connection con = JDBC.getConnection()) {
            for (int i = 0; i < Mate.TABLE_NUM; i++) {
                String sql = "drop table t_member_" + i + ";";
                try (PreparedStatement ps = con.prepareStatement(sql)) {
                    ps.execute();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("table drop sql=" + sql);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
